package com.gugee.ins.deploy.consumer.blogger.controller;


import com.gugee.ins.common.beanutil.InsBloggerConversion;
import com.gugee.ins.common.dal.ai.AiAnalysisClient;
import com.gugee.ins.common.dal.service.blogger.InsBloggerDao;
import com.gugee.ins.common.dal.service.blogger.InsBloggerMainDao;
import com.gugee.ins.common.dal.service.brand.InsBrandDao;
import com.gugee.ins.common.dal.service.dic.InsDicCategoryDao;
import com.gugee.ins.common.dal.service.product.InsProductDao;
import com.gugee.ins.common.dal.service.spider.SpiderSvc;
import com.gugee.ins.common.dal.spider.InsBloggerSpiderClient;
import com.gugee.ins.common.dal.spider.InsMediaCommentSpiderClient;
import com.gugee.ins.common.dal.spider.InsMediaSpiderClient;
import com.gugee.ins.common.model.ai.*;
import com.gugee.ins.common.model.mysql.blogger.InsBlogger;
import com.gugee.ins.common.model.mysql.dic.InsDicCategory;
import com.gugee.ins.common.model.spider.app.media.InsAppGetProductMediaResponse;
import com.gugee.ins.common.model.spider.app.media.InsAppMarkedMediaResponse;
import com.gugee.ins.common.model.spider.app.media.InsAppMediaResponse;
import com.gugee.ins.common.model.spider.blogger.*;
import com.gugee.ins.common.model.spider.comment.EdgeMediaCommentBody;
import com.gugee.ins.common.model.spider.media.EdgeMediaBody;
import com.gugee.ins.data.analysis.service.InsAnalysisBrandCategoryService;
import com.gugee.ins.data.analysis.service.InsAnalysisBrandService;
import com.gugee.ins.data.analysis.service.InsAnalysisProductCategoryService;
import com.gugee.ins.data.collector.bloggerfollowing.service.InsBloggerFollowingService;
import com.gugee.ins.data.collector.media.service.InsMediaService;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.gugee.ins.data.collector.blogger.service.InsBloggerCollectService;

import com.gugee.ins.data.common.msg.InsBrandMessage;
import com.gugee.ins.data.common.msg.InsMarkMessage;
import com.gugee.ins.data.common.msg.InsProductMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class BloggerControllerTest {

    @Autowired
    @Qualifier("kafkaTemplateInsBlogger")
    private KafkaTemplate<Long, InsBloggerMessage> kafkaTemplateInsBlogger;

    @Autowired
    @Qualifier("kafkaTemplateInsMarkDay")
    private KafkaTemplate<Long, InsMarkMessage> kafkaTemplateInsMarkDay;

    @Autowired
    InsBloggerSpiderClient insBloggerSpiderClient;
    /**
     * 爬虫接口
     */
    @Autowired
    SpiderSvc spiderSvc;

    @Autowired
    InsMediaSpiderClient insMediaSpiderClient;

    @Autowired
    InsMediaCommentSpiderClient insMediaCommentSpiderClient;

    @Autowired
    InsBloggerDao insBloggerDao;

    @Autowired
    InsBloggerMainDao insBloggerMainDao;

    @Autowired
    InsProductDao insProductDao;
    /**
     * ai
     */
    @Autowired
    AiAnalysisClient aiAnalysisClient;

    /**
     *
     */
    @Autowired
    InsBloggerCollectService insBloggerCollectService;

    /**
     *
     */
    @Autowired
    InsMediaService insMediaService;

    @Autowired
    InsDicCategoryDao insDicCategoryDao;

    /**
     *
     */
    @Autowired
    InsBloggerFollowingService insBloggerFollowingService;


    @GetMapping(path = "/spiderBySuggested")
    public InsBloggerSuggestedResponse spiderBySuggested() {
        InsBloggerSuggestedResponse insBloggerSuggestedResponse = insBloggerSpiderClient.getSuggestBlogger();
        return insBloggerSuggestedResponse;
    }

    @GetMapping(path = "/spiderByUserId/{uid}")
    public InsBlogger spiderByUserId(@PathVariable Long uid) {
        InsBloggerByIdResponse insBloggerByIdResponse = spiderSvc.getInsBloggerByUid(uid);
        InsBlogger insBlogger = InsBloggerConversion.mapper(insBloggerByIdResponse.getUser());
        return insBlogger;
    }

    @GetMapping(path = "/spiderFollowing/{uid}/{count}/{end}")
    public InsBloggerFollowingResponse spiderFollowing(@PathVariable long uid,@PathVariable int count,@PathVariable String end) {
        if("1".equals(end)){
            end = "";
        }
        InsBloggerFollowingResponse insBloggerFollowingResponse = insBloggerSpiderClient.getFollowingBloggerByUid(uid,count,end);
        return insBloggerFollowingResponse;
    }

    @GetMapping(path = "/spiderFollowed/{uid}/{count}/{end}")
    public InsBloggerFollowedResponse spiderFollowed(@PathVariable long uid,@PathVariable int count,@PathVariable String end) {
        if("1".equals(end)){
            end = "";
        }
        InsBloggerFollowedResponse insBloggerFollowedResponse = insBloggerSpiderClient.getFollowedBloggerByUid(uid,count,end);
        return insBloggerFollowedResponse;
    }

    @GetMapping(path = "/spiderDetail/{uid}")
    public InsBloggerDetailResponse spiderDetail(@PathVariable Long uid) {
        InsBloggerDetailResponse insBloggerDetailResponse = insBloggerSpiderClient.getInsBloggerDetail(uid);
        return insBloggerDetailResponse;
    }

    @GetMapping(path = "/spiderMedia/{uid}/{count}/{end}")
    public EdgeMediaBody spiderMedia(@PathVariable long uid,@PathVariable int count,@PathVariable String end) {
        if("1".equals(end)){
            end = "";
        }
        EdgeMediaBody edgeMediaBody = insMediaSpiderClient.getMediaByUid(uid,count,end);
        return edgeMediaBody;
    }

    @GetMapping(path = "/spiderAppMedia/{uid}/{end}")
    public InsAppMediaResponse spiderAppMedia(@PathVariable long uid,@PathVariable String end) {
        if("1".equals(end)){
            end = "";
        }
        InsAppMediaResponse edgeMediaBody = insMediaSpiderClient.getAppMediaByUid(uid,end);
        return edgeMediaBody;
    }

    @GetMapping(path = "/spiderGetProductMarkedMediaList/{productId}/{end}")
    public InsAppGetProductMediaResponse spiderGetProductMarkedMediaList(@PathVariable long productId,@PathVariable String end) {
        if("1".equals(end)){
            end = "";
        }
        InsAppGetProductMediaResponse edgeMediaBody = insMediaSpiderClient.getProductMarkedMediaList(productId,end);
        return edgeMediaBody;
    }

    @GetMapping(path = "/spiderComment/{shortCode}/{count}/{end}")
    public EdgeMediaCommentBody spiderComment(@PathVariable String shortCode,@PathVariable int count,@PathVariable String end) {
        if("1".equals(end)){
            end = "";

        }
        EdgeMediaCommentBody edgeMediaCommentBody = insMediaCommentSpiderClient.getMediaComments(shortCode,count,end);
        return edgeMediaCommentBody;
    }

    @GetMapping(path = "/beginCollect/{uid}")
    public void beginCollect(@PathVariable long uid) {
        String userName = "philipp.frank.art";
        String picUrl = "https://scontent-sjc3-1.cdninstagram.com/v/t51.2885-19/s150x150/79376590_460356608234321_5602248452051304448_n.jpg?_nc_ht=scontent-sjc3-1.cdninstagram.com&_nc_ohc=yH_w1NlD1ZoAX_Ga1pv&tp=1&oh=088d2513a5fc82ba09a13991fec53a93&oe=60072570";
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        insBloggerMessage.setUserName(userName);
        insBloggerMessage.setPicUrl(StringUtils.isEmpty(picUrl) ? "" : picUrl);
        kafkaTemplateInsBlogger.send("insBlogger_cl_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendUpdateBlogger/{uid}")
    public void sendUpdateBlogger(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insBloggerUpdate_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendUpdateMBlogger/{uid}")
    public void sendUpdateMBlogger(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insMBlogger_update_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendClMBlogger/{uid}")
    public void sendClMBlogger(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insMBlogger_cl_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendMedia/{uid}")
    public void sendMedia(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insMedia_day",insBloggerMessage);
    }

    @GetMapping(path = "/callMedia/{uid}")
    public void callMedia(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        List<InsBloggerMessage> insBloggerMessages = new ArrayList<>(1);
        insBloggerMessages.add(insBloggerMessage);
        insMediaService.handleInsMediaCollect(insBloggerMessages);
    }

    @GetMapping(path = "/sendComment/{uid}")
    public void sendComment(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insCommentCollect_day",insBloggerMessage);
    }
    @GetMapping(path = "/sendFollowing/{uid}")
    public void sendFollowing(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insBloggerFollowing_day",insBloggerMessage);
    }

    @GetMapping(path = "/callFollowing/{uid}")
    public void callFollowing(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        List<InsBloggerMessage> insBloggerMessages = new ArrayList<>(1);
        insBloggerMessages.add(insBloggerMessage);
        insBloggerFollowingService.handleActiveFollowerFollowingCollect(insBloggerMessages);
    }

    @GetMapping(path = "/sendFollower/{uid}")
    public void sendFollower(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insBloggerFollower_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendAdMediaIdentify/{uid}")
    public void sendAdMediaIdentify(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insAdMediaIdentify_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendLocation/{uid}")
    public void sendLocation(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insBloggerLocation_day",insBloggerMessage);
    }
    @GetMapping(path = "/sendInsAnalysisAvatar/{uid}")
    public void sendInsAnalysisAvatar(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        insBloggerMessage.setPicUrl(insBloggerDao.findByUid(uid).getPicUrl());
        kafkaTemplateInsBlogger.send("insAnalysisAvatar_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendInsAnalysisLocation/{uid}")
    public void sendInsAnalysisLocation(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insAnalysisLocation_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendInsAnalysisLanguage/{uid}")
    public void sendInsAnalysisLanguage(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insAnalysisLanguage_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendInsAnalysisIncome/{uid}")
    public void sendInsAnalysisIncome(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insAnalysisIncome_day",insBloggerMessage);
    }

    @GetMapping(path = "/analysisLanguage")
    public void analysisLanguage() {
        Map<String,String> aiAnalysisMap = new HashMap<>(10);
        aiAnalysisMap.put("123","12");
        aiAnalysisMap.put("1234_3","asdfasdfsad");
        aiAnalysisMap.put("1233_5","会啊范德萨");
        Map<String,String> result = aiAnalysisClient.getLanguageByText(aiAnalysisMap);
        System.out.println(result);
    }

    @GetMapping(path = "/analysisIncome")
    public void analysisIncome() {
        List<InComeRequest> list = new ArrayList<>();
        InComeRequest inComeRequest = new InComeRequest();
        inComeRequest.setAge(1);
        inComeRequest.setId(123);
        inComeRequest.setGender(0);
        inComeRequest.setCountry("United States");
        list.add(inComeRequest);
        List<InComeResponse> responses = aiAnalysisClient.getIncome(list);
        System.out.println(responses);
    }

    @GetMapping(path = "/markDay")
    public void markDay() {
        List<Long> uids = new ArrayList<>(4);
        uids.add(27467826L);
        uids.add(5200199L);
        uids.add(8012033210L);
        uids.add(25025320L);
        InsMarkMessage insMarkMessage = new InsMarkMessage();
        insMarkMessage.setPage(1);
        insMarkMessage.setPageSize(200);
        insMarkMessage.setUids(uids);
        kafkaTemplateInsMarkDay.send("insMarkDay_day",insMarkMessage);
    }

    @GetMapping(path = "/badSqlLog")
    public void badSqlLog() {
        InsDicCategory insDicCategory1 = new InsDicCategory();
        insDicCategory1.setId(232323);
        insDicCategory1.setCategory("Zoo");
        insDicCategory1.setOverallCategory("Animals");
        insDicCategoryDao.save(insDicCategory1);
    }

    @GetMapping(path = "/badSqlLogBatch")
    public void badSqlLogBatch() {
        InsDicCategory insDicCategory2 = new InsDicCategory();
        InsDicCategory insDicCategory3 = new InsDicCategory();
        insDicCategory2.setId(2222222);
        insDicCategory2.setCategory("Veterinarian");
        insDicCategory2.setOverallCategory("Animals");
        insDicCategory3.setId(3333333);
        insDicCategory3.setCategory("Pet Sitter");
        insDicCategory3.setOverallCategory("Animals");
        List<InsDicCategory> list = new ArrayList<>(3);
        list.add(insDicCategory2);
        list.add(insDicCategory3);
        insDicCategoryDao.saveOrUpdateBatch(list);
    }

    @GetMapping(path = "/sendStProduct/{productId}/{typeCode}")
    public void sendStProduct(@PathVariable long productId,@PathVariable int typeCode) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(productId);
        insProductDao.findByProductId(productId);
        insBloggerMessage.setUserName(insProductDao.findByProductId(productId).getOverallCategoryName());
        insBloggerMessage.setTypeCode(typeCode);
        kafkaTemplateInsBlogger.send("insProductBaseSt_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendAutoMsg/{uid}/{topic}")
    public void sendAutoMsg(@PathVariable long uid,@PathVariable String topic) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send(topic,insBloggerMessage);
    }

    @GetMapping(path = "/sendAnalysisMediaScore/{uid}")
    public void sendAnalysisMediaScore(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insAnalysisMediaScore_day",insBloggerMessage);
    }

    @GetMapping(path = "/spiderAppMarkedMediaByUserId/{uid}/{end}")
    public InsAppMarkedMediaResponse spiderAppMarkedMediaByUserId(@PathVariable long uid, @PathVariable String end) {
        if("1".equals(end)){
            end = "";

        }
        InsAppMarkedMediaResponse edgeMediaCommentBody = insMediaSpiderClient.getMarkedMediaByUserId(uid,end);
        return edgeMediaCommentBody;
    }

    @GetMapping(path = "/analysisCategory")
    public InsBloggerCategoryResponse analysisCategory() {
        List<InsBloggerMediaTag> insBloggerMediaTags = new ArrayList<>(2);
        InsBloggerMediaTag insBloggerMediaTag1 = new InsBloggerMediaTag();
        InsBloggerMediaTag insBloggerMediaTag2 = new InsBloggerMediaTag();
        insBloggerMediaTag1.setId(1);
        insBloggerMediaTag2.setId(2);
        List<List<String>> list1 = new ArrayList<>();
        List<List<String>> list2 = new ArrayList<>();
        List<String> list11 = new ArrayList<>();
        List<String> list22 = new ArrayList<>();
        list11.add("긁긁");
        list11.add("냥스타그램");
        list11.add("고양이");
        list11.add("catstagram");
        list11.add("cat");
        list11.add("SPONSORED");
        list22.add("adidasRYV ");
        list1.add(list11);
        list2.add(list22);
        insBloggerMediaTag1.setTags(list1);
        insBloggerMediaTag2.setTags(list2);
        insBloggerMediaTags.add(insBloggerMediaTag1);
        insBloggerMediaTags.add(insBloggerMediaTag2);
        return aiAnalysisClient.getCategoryByText(insBloggerMediaTags);
    }

    @GetMapping(path = "/sendAnalysisCategory/{uid}")
    public void sendAnalysisCategory(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insAnalysisCategory_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendAccountUnusual/{uid}")
    public void sendAccountUnusual(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insBloggerAccountUnusual_day",insBloggerMessage);
    }

    @GetMapping(path = "/sendAccountNormal/{uid}")
    public void sendAccountNormal(@PathVariable long uid) {
        InsBloggerMessage insBloggerMessage = new InsBloggerMessage();
        insBloggerMessage.setUid(uid);
        kafkaTemplateInsBlogger.send("insBloggerAccountNormal_day",insBloggerMessage);
    }

    @Autowired
    InsAnalysisBrandService insAnalysisBrandService;
    @GetMapping(path = "/analysisBrand/{id}")
    public void analysisBrand(@PathVariable long id) {
        List<InsBloggerMessage> insBloggerMessages = insBloggerMainDao.findAnalysisBrandBloggerByPages(id,id,1647512141);
        insAnalysisBrandService.handleAnalysisBrand(insBloggerMessages);
    }

    @Autowired
    InsAnalysisBrandCategoryService insAnalysisBrandCategoryService;
    @Autowired
    InsBrandDao insBrandDao;

    @GetMapping(path = "/analysisBrandCategory/{id}")
    public void analysisBrandCategory(@PathVariable int id) {
        List<InsBrandMessage> rst = insBrandDao.findInsAnalysisBrandCategoryMessageByPages(id,id,1647512141);
        insAnalysisBrandCategoryService.handleAnalysisBrandCategory(rst);
    }

    @Autowired
    InsAnalysisProductCategoryService insAnalysisProductCategoryService;
    @GetMapping(path = "/analysisProductCategory/{id}")
    public void analysisProductCategory(@PathVariable int id) {
        List<InsProductMessage> rst = insProductDao.findInsAnalysisProductCategoryMsgByPages(id,id,1647512141);
        insAnalysisProductCategoryService.handle(rst);
    }
}


